//
//  ItemMeunView.m
//  XHUI
//
//  Created by 郑信鸿 on 16/11/18.
//  Copyright © 2016年 郑信鸿. All rights reserved.
//

#import "ItemMeunView.h"

#define kCenterGap 2
#define kTopBottomRate 0.5
#define kBottomFont [UIFont systemFontOfSize:14]

@interface ItemMeunView ()


@property(nonatomic, strong)UITapGestureRecognizer *tap;
@property(nonatomic, strong)UILabel *topLabel;
@property(nonatomic, strong)UILabel *bottomLabel;
@property(nonatomic, strong)UIImageView *imageView;

@end

@implementation ItemMeunView

- (void)layoutSubviews{
    
    [super layoutSubviews];
    
    if (self.bottomLabel != nil) {
        
        [self configFrame];
    }
  
}


- (instancetype)initWithFrame:(CGRect)frame topTitle:(NSString *)topTitle bottomTitle:(NSString *)bottomTitle{
    
    if (self = [super initWithFrame:frame]) {
        [self bulidSubViews];
        self.topTitle = topTitle;
        self.bottomTitle = bottomTitle;
         [self configDefaltProperty];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame imageName:(NSString *)imageName bottomTitle:(NSString *)bottomTitle{
    
    if (self = [super initWithFrame:frame]) {
        [self bulidSubViews];
        self.image = [UIImage imageNamed:imageName];
        self.bottomTitle = bottomTitle;
         [self configDefaltProperty];
    }
    return self;
    
}


- (void)loadTopTitle:(NSString *)topTitle bottomTitle:(NSString *)bottomTitle{
    
    if (self.bottomLabel == nil) {
        [self bulidSubViews];
    }
    self.topTitle = topTitle;
    self.bottomTitle = bottomTitle;
    [self configDefaltProperty];
    [self setNeedsLayout];
}


- (void)loadImageName:(NSString *)imageName bottomTitle:(NSString *)bottomTitle{
    if (self.bottomLabel == nil) {
        [self bulidSubViews];
    }
    self.image = [UIImage imageNamed:imageName];
    self.bottomTitle = bottomTitle;
    [self configDefaltProperty];
    [self setNeedsLayout];
}

- (void)configDefaltProperty{
    
    self.meunFont = kBottomFont;
    self.meunTitleColor = [UIColor blackColor];
    self.centerGap = kCenterGap;
}

- (void)configFrame{
    
    CGFloat height = self.bounds.size.height;
    CGFloat width = self.bounds.size.width;
    NSDictionary *attrs = @{NSFontAttributeName : self.bottomFont};
    CGSize maxSize = CGSizeMake(MAXFLOAT, MAXFLOAT);
    CGSize bottomSize = [self.bottomTitle boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
    CGSize topSize;
    if (self.topTitle != nil) {
        
        NSDictionary *attrs = @{NSFontAttributeName : self.topFont};
       
        topSize = [self.topTitle boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;
        
    }else{
        
        topSize = self.image.size;
    }
   
    CGFloat y = (height - (topSize.height + bottomSize.height + _centerGap))/2;
    CGRect topFrame = CGRectMake((width - topSize.width)/2, y, topSize.width, topSize.height);
    self.bottomLabel.frame = CGRectMake(0, CGRectGetMaxY(topFrame)+_centerGap, width, bottomSize.height);
    self.topLabel.frame = topFrame;
    self.imageView.frame = topFrame;

}


- (void)bulidSubViews{
    
    self.imageView = [[UIImageView alloc] init];
    self.imageView.hidden = YES;
    [self addSubview:self.imageView];
    self.topLabel = [[UILabel alloc] init];
    self.topLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.topLabel];
    self.bottomLabel = [[UILabel alloc] init];
    self.bottomLabel.adjustsFontSizeToFitWidth = YES;
    self.bottomLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.bottomLabel];
}


- (void)tapAction{
    
    if ([_delegate respondsToSelector:@selector(itemMeunViewAction:)]) {
        
        [_delegate itemMeunViewAction:self];
    }
    
}

#pragma mark -setter

- (void)setDelegate:(id<ItemMeunViewDelegate>)delegate{
    
    _delegate = delegate;
    self.userInteractionEnabled = YES;
    [self removeGestureRecognizer:_tap];
    if (_tap == nil) {
        
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction)];
        
    }
     [self addGestureRecognizer:_tap];
}


- (void)setMeunTitleColor:(UIColor *)meunTitleColor{
    _meunTitleColor = meunTitleColor;
    self.topLabel.textColor = meunTitleColor;
    self.bottomLabel.textColor = meunTitleColor;
    
}

- (void)setTopTitleColor:(UIColor *)topTitleColor{
    _topTitleColor = topTitleColor;
    self.topLabel.textColor = topTitleColor;
}

- (void)setBottomTitleColor:(UIColor *)bottomTitleColor{
    _bottomTitleColor = bottomTitleColor;
    self.bottomLabel.textColor = bottomTitleColor;
}

- (void)setImage:(UIImage *)image{
    
    _image = image;
    self.topTitle = nil;
    self.imageView.image = image;
    self.imageView.hidden = NO;
    self.topLabel.hidden = YES;
    [self setNeedsLayout];
}

- (void)setTopTitle:(NSString *)topTitle{
    
    _topTitle = topTitle;
    self.topLabel.text = topTitle;
    self.imageView.hidden = YES;
    self.topLabel.hidden = NO;
    [self setNeedsLayout];
}

- (void)setBottomTitle:(NSString *)bottomTitle{
    _bottomTitle = bottomTitle;
    self.bottomLabel.text = bottomTitle;
}


- (void)setTopFont:(UIFont *)topFont{
    _topFont = topFont;
    self.topLabel.font = topFont;
    [self setNeedsLayout];
}

- (void)setBottomFont:(UIFont *)bottomFont{
    _bottomFont = bottomFont;
    self.bottomLabel.font = bottomFont;
    [self setNeedsLayout];
}


- (void)setMeunFont:(UIFont *)meunFont{
    _meunFont = meunFont;
    self.topFont = meunFont;
    self.bottomFont = meunFont;
    self.topLabel.font = meunFont;
    self.bottomLabel.font = meunFont;
    [self setNeedsLayout];
}

- (void)setCenterGap:(CGFloat)centerGap{
    _centerGap = centerGap;
    [self setNeedsLayout];
}

@end
